﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Json;
using System.IO;

namespace Shopping.Common
{
    public class JSONHelper
    {
        #region "将可序列的类转化Json数据格式； by 赵国伟；2009-10-15"
        /// <summary>
        /// 将可序列的类转化Json数据格式；[采用.net3.5自带的json支持类]
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static string ObjectToJson<T>(T obj)
        {
            if (obj == null)
                return "";
            DataContractJsonSerializer ds = new DataContractJsonSerializer(typeof(T));
            MemoryStream ms = new MemoryStream();
            ds.WriteObject(ms, obj);
            string strJSON = Encoding.UTF8.GetString(ms.ToArray());
            ms.Close();
            return strJSON;
        }

        #endregion

        #region "将指定的Json字符串转化为指定的实体类； by 赵国伟 2009-10-15"
        /// <summary>
        /// 将指定的Json字符串转化为指定的实体类；[采用.net3.5自带的json支持类]
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="sJson"></param>
        /// <returns></returns>
        public static T JsonStringToObject<T>(string sJson) where T : class
        {
            DataContractJsonSerializer ds = new DataContractJsonSerializer(typeof(T));
            MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(sJson));
            T obj = (T)ds.ReadObject(ms);

            ms.Close();
            return obj;
        }

        #endregion
    }
}
